<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../../js/vue.js"></script>
</head>

<body>
    <div id="app">
        <child></child>
        <child2></child2>
    </div>
</body>

<template id="child">
    <div>
        {{num}}
    </div>
</template>

<template id="child2">
    <div>
        <button @click="add">+</button>
    </div>
</template>

<script>

    var bus = new Vue();//弄一个空的实例

    let child = {
        template: "#child",
        data() {
            return {
                num: 10,
            }
        },
        created() {
            console.log(this);
            bus.$on("add", (n) => { //用$on绑定事件，和JQ里面的on差不多
                this.num += n;
            })
        },
    }

    let child2 = {
        template: "#child2",
        methods: {
            add() {
                bus.$emit("add", 10);
            }
        }
    }

    new Vue({
        el: "#app",
        data: {
            num: 10,
        },
        methods: {
            getparent(msg) {
                alert(msg);
            },
            getchild() {
                console.log(this.$refs.child.msg);
            }

        },
        components: {
            child,
            child2
        }
    })
</script>

</html>